<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    @closed="dialogClose"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="140px"
    >
      <el-form-item label="广告名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="广告名称"></el-input>
      </el-form-item>
      <el-form-item label="广告图片" prop="pic">
        <single-upload v-model="dataForm.pic"></single-upload>
      </el-form-item>
      <el-form-item label="图片链接" prop="url">
        <el-input v-model="dataForm.url" placeholder="图片链接"></el-input>
      </el-form-item>
      <el-form-item label="广告位置" prop="type">
        <el-select v-model="dataForm.type">
          <el-option
            v-for="item in defaultTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model.number="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
      <el-form-item label="上线/下线：">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">下线</el-radio>
          <el-radio :label="1">上线</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="广告备注：">
        <el-input
          type="textarea"
          :rows="5"
          placeholder="请输入内容"
          v-model="dataForm.note"
        >
        </el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "@/components/upload/singleUpload";
import AdvertiseApi from "@/api/marketing/advertise";

export default {
  components: { SingleUpload },
  data() {
    return {
      defaultTypeOptions: [
        {
          label: "PC首页轮播",
          value: 0,
        },
        {
          label: "APP首页轮播",
          value: 1,
        },
      ],
      visible: false,
      dataForm: {},
      dataRule: {
        name: [{ required: true, message: "品牌名不能为空", trigger: "blur" }],
        pic: [
          { required: true, message: "品牌logo地址不能为空", trigger: "blur" },
        ],
        sort: [
          {
            validator: (rule, value, callback) => {
              if (value === "") {
                callback(new Error("排序字段必须填写"));
              } else if (!Number.isInteger(value) || value < 0) {
                callback(new Error("排序必须是一个大于等于0的整数"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.visible = true;
      if (id) {
        this.dataForm.id = id;
        this.$nextTick(() => {
          AdvertiseApi.getAdvertise(id).then(({ data }) => {
            this.dataForm = data;
          });
        });
      }
    },
    dialogClose() {
      this.dataForm = {};
      this.$refs["dataForm"].resetFields();
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          if (this.dataForm.id) {
            AdvertiseApi.updateAdvertise(this.dataForm).then(({ data }) => {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            });
          } else {
            AdvertiseApi.createAdvertise(this.dataForm).then(({ data }) => {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            });
          }
        }
      });
    },
  },
};
</script>
